package com.yun.zf.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicParameters;
import com.yun.zf.admin.service.ZfPactPersonInfoService;
import com.yun.zf.admin.service.ZfPactRentInfoService;
import com.yun.zf.admin.service.ZfPactService;
import com.yun.zf.common.admin.model.pact.PactBasicMo;
import com.yun.zf.common.admin.model.pact.PactPersonListMo;
import com.yun.zf.common.admin.model.pact.PactRentInfoMo;
import com.yun.zf.common.admin.model.pact.PactSearchMo;
import com.yun.zf.common.annotation.LogPrint;
import com.yun.zf.common.constant.Constants;
import com.yun.zf.common.vo.Response;
import com.yun.zf.system.utils.AliyunOssUtil;
import freemarker.template.Template;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/pact")
@Api(tags = "签约管理")
@Slf4j
public class PactController extends BaseController {
    @Autowired
    private ZfPactService pactService;
    @Autowired
    private ZfPactPersonInfoService personInfoService;
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;
    @Autowired
    private ZfPactRentInfoService rentInfoService;
    @Autowired
    private AliyunOssUtil ossUtil;



    @PostMapping("/newPact")
    @PreAuthorize("@ss.hasAnyRoles('agent')")
    @ApiOperation(value = "创建合同基本信息")
    @LogPrint(description = "创建合同基本信息")
    public Response newPact(@RequestBody PactBasicMo basicMo) {
        try {
            return Response.success(pactService.addNewPact(basicMo, aCode, prov));
        } catch (Exception e) {
            return Response.error("创建合同失败");
        }
    }

    @PostMapping("/signatory")
    @PreAuthorize("@ss.hasAnyRoles('agent')")
    @ApiOperation(value = "添加签约人信息")
    @LogPrint(description = "添加签约人信息")
    public Response signatory(@RequestBody PactPersonListMo persons) {
        try {
            personInfoService.addPactPerson(persons, prov, aCode);
            return Response.success();
        } catch (Exception e) {
            return Response.error("签约人保存失败");
        }
    }
    @PostMapping("/getSignatory")
    @PreAuthorize("@ss.hasAnyRoles('agent')")
    @ApiOperation(value = "查看签约人信息")
    @LogPrint(description = "查看签约人信息")
    @DynamicParameters(name = "SendOrderMapModel",properties = {
            @DynamicParameter(name = "pactId",value = "合同编号",example = "111",required = true)
    })
    public Response getSignatory(@RequestBody Map<String,String> map) {
        try {
            return Response.success(personInfoService.getPactPersonsDetail( prov, aCode,Long.parseLong(map.get("pactId"))));
        } catch (Exception e) {
            return Response.error("查询信息失败");
        }
    }


    @PostMapping("/uploadPactFile")
    @ApiOperation(value = "上传签约人证件以及补充文件信息")
    @LogPrint(description = "上传签约人证件以及补充文件信息")
    public Response uploadPactFile(@RequestParam("files") MultipartFile[] files, HttpServletRequest request) {
        try {
            List<Map<String, String>> pictureList = new ArrayList<>();
            if (files != null && files.length > 0) {
                for (MultipartFile file : files
                ) {
                    Map<String, String> map = ossUtil.uploadObject2OSS(file, Constants.BUCKET_PACT, Constants.FOLDER_PACT + "/" + request.getParameter("pactId"));
                    log.info("小区图片上传返回信息-----{}", JSONObject.toJSONString(map));
                    if (map != null) {
                        pictureList.add(map);
                    }
                }
            }
            return Response.success(pictureList);
        } catch (Exception e) {
            return Response.error();
        }
    }

    @PostMapping("/rent")
    @PreAuthorize("@ss.hasAnyRoles('agent')")
    @ApiOperation(value = "租赁补充信息")
    @LogPrint(description = "租赁补充信息")
    public Response rent(@RequestBody PactRentInfoMo rentInfoMo) {
        try {
            rentInfoService.editRentInfo(prov,aCode,rentInfoMo);
            return Response.success();
        } catch (Exception e) {
            return Response.error("租赁信息补充失败");
        }
    }

    @PostMapping("/getRent")
    @PreAuthorize("@ss.hasAnyRoles('agent')")
    @ApiOperation(value = "查看租赁信息")
    @LogPrint(description = "查看租赁信息")
    @DynamicParameters(name = "SendOrderMapModel",properties = {
            @DynamicParameter(name = "pactId",value = "合同编号",example = "111",required = true)
    })
    public Response getRent(@RequestBody  Map<String,String> map) {
        try {
            return Response.success(rentInfoService.pactRentInfo(prov,aCode,Long.parseLong(map.get("pactId"))));
        } catch (Exception e) {
            return Response.error("租赁信息获取失败");
        }
    }

    @PostMapping("/pactDetail")
    @PreAuthorize("@ss.hasAnyRoles('agent')")
    @ApiOperation(value = "合同详情")
    @LogPrint(description = "合同详情")
    @DynamicParameters(name = "SendOrderMapModel", properties = {
            @DynamicParameter(name = "pactId", value = "合同编号", example = "000111")
    })
    public Response pactDetail(@RequestBody Map<String, String> map) {
        return Response.success(pactService.pactDetail(aCode, prov, Long.parseLong(map.get("pactId"))));

    }

    @PostMapping("/pactPreview")
    @PreAuthorize("@ss.hasAnyRoles('agent')")
    @ApiOperation(value = "合同预览")
    @LogPrint(description = "合同预览")
    @DynamicParameters(name = "SendOrderMapModel", properties = {
            @DynamicParameter(name = "pactId", value = "合同编号", example = "000111"),
            @DynamicParameter(name = "orderId", value = "订单号", example = "000111")
    })
    public Response pactPreview(@RequestBody Map<String, String> map) {
        try {
            Map<String, Object> temp = pactService.previewPact(prov, aCode, Long.parseLong(map.get("pactId")));
            Template template = freeMarkerConfigurer.getConfiguration().getTemplate("index.ftl");
            String resp = FreeMarkerTemplateUtils.processTemplateIntoString(template, temp);
            Map<String, String> res = new HashMap<>();
            res.put("context", resp);
            res.put("pactId", map.get("pactId"));
            res.put("orderId", map.get("orderId"));
            return Response.success(res);
        } catch (Exception e) {
            return Response.error();
        }
    }

    @PostMapping("/pactToPdf")
    @PreAuthorize("@ss.hasAnyRoles('agent')")
    @ApiOperation(value = "合同生成PDF")
    @LogPrint(description = "合同生成PDF")
    @DynamicParameters(name = "SendOrderMapModel", properties = {
            @DynamicParameter(name = "pactId", value = "合同编号", example = "000111"),
            @DynamicParameter(name = "orderId", value = "订单号", example = "000111")
    })
    public Response pactToPdf(@RequestBody Map<String, String> map) {
        try {
            pactService.pactToPdf(prov, aCode, Long.parseLong(map.get("pactId")), Long.parseLong(map.get("orderId")));

            return Response.success();
        } catch (Exception e) {
            return Response.error();
        }
    }

    @PostMapping("/searchPact")
    @PreAuthorize("@ss.hasAnyRoles('agent')")
    @ApiOperation(value = "合同搜索")
    @LogPrint(description = "合同搜索")
    public Response searchPact(@RequestBody Map<String, String> map) {
        try {
            return Response.success();
        } catch (Exception e) {
            return Response.error();
        }
    }

    @PostMapping("/myPact")
    @PreAuthorize("@ss.hasAnyRoles('agent')")
    @ApiOperation(value = "我的合同")
    @LogPrint(description = "我的合同")
    public Response myPact(@RequestBody PactSearchMo pactSearchMo) {
        try {
            return Response.success(pactService.myPact(prov,aCode,pactSearchMo));
        } catch (Exception e) {
            return Response.error();
        }
    }
}
